import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useUserStore } from '../stores/user'
import { useMerchantStore } from '../stores/merchant'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/merchant/:id',
      name: 'merchant',
      component: () => import('../views/MerchantView.vue'),
      meta: {
        title: '商家详情'
      }
    },
    {
      path: '/dish/:id',
      name: 'dish',
      component: () => import('../views/DishView.vue'),
      meta: {
        title: '菜品详情'
      }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
      meta: {
        title: '购物车',
        requiresAuth: true
      }
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('../views/OrderView.vue'),
      meta: {
        title: '订单',
        requiresAuth: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: {
        title: '注册'
      }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/UserView.vue'),
      meta: {
        title: '个人中心',
        requiresAuth: true
      }
    },
    // 商家相关路由
    {
      path: '/merchant/login',
      name: 'merchantLogin',
      component: () => import('../views/MerchantLoginView.vue'),
      meta: {
        title: '商家登录'
      }
    },
    {
      path: '/merchant/register',
      name: 'merchantRegister',
      component: () => import('../views/MerchantRegisterView.vue'),
      meta: {
        title: '商家注册'
      }
    },
    {
      path: '/merchant/dashboard',
      name: 'merchantDashboard',
      component: () => import('../views/MerchantDashboardView.vue'),
      meta: {
        title: '商家管理中心',
        requiresMerchantAuth: true
      }
    },
    {
      path: '/merchant/dishes',
      name: 'merchantDishes',
      component: () => import('../views/MerchantDishesView.vue'),
      meta: {
        title: '菜品管理',
        requiresMerchantAuth: true
      }
    },
    {
      path: '/merchant/orders',
      name: 'merchantOrders',
      component: () => import('../views/MerchantOrdersView.vue'),
      meta: {
        title: '订单管理',
        requiresMerchantAuth: true
      }
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title || '康赟点餐'} - 康赟点餐系统`
  
  // 判断该路由是否需要用户登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const userStore = useUserStore()
    if (!userStore.isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将要跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      next()
    }
  }
  // 判断该路由是否需要商家登录权限
  else if (to.matched.some(record => record.meta.requiresMerchantAuth)) {
    const merchantStore = useMerchantStore()
    if (!merchantStore.isLoggedIn) {
      next({
        path: '/merchant/login',
        query: { redirect: to.fullPath } // 将要跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
